package com.tanhua.app.controller;

import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.app.service.SettingsService;
import com.tanhua.app.service.UserInfoService;
import com.tanhua.app.service.UserService;
import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private SettingsService settingsService;

    @Autowired
    private UserService userService;

    //查询用户信息
    @GetMapping
    public ResponseEntity userInfo(Long userId){
//        //1.判断token是否合法
//        boolean verifyToken = JwtUtils.verifyToken(token);
//        if (!verifyToken) {
//            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(null);
//        }
//        //2.获取用户id
//        Claims claims = JwtUtils.getClaims(token);
//        Long id =  Long.parseLong(claims.get("id").toString());

        if (userId == null){
            userId = UserHolder.getUserId();
        }
        //3.调用service层
        UserInfoVo userInfo = userInfoService.findById(userId);
        //返回结果
        return ResponseEntity.ok(userInfo);
    }

    //更新用户信息
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo,@RequestHeader("Authorization") String token){
        //1.判断token是否合法
        boolean verifyToken = JwtUtils.verifyToken(token);
        if (!verifyToken) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(null);
        }
        //2.获取用户id
        Claims claims = JwtUtils.getClaims(token);
        Long id =  Long.parseLong(claims.get("id").toString());
        //Long id = UserHolder.getUserId();
        userInfo.setId(id);
        //3.调用service层
        userInfoService.update(userInfo);
        //4.返回结果
        return ResponseEntity.ok(null);
    }

    //查询通用设置
    @GetMapping("/settings")
    public ResponseEntity settings(){
        //1.调用service层方法
        SettingsVo vo = settingsService.settings();
        //2.返回结果
        return ResponseEntity.ok(vo);
    }

    //设置陌生人问题
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map){
        //1.读取参数
        String content = (String) map.get("content");
        //2.调用service方法
        settingsService.questions(content);
        //3.返回结果
        return ResponseEntity.ok(null);
    }

    //保存通用设置
    @PostMapping("/notifications/setting")
    public ResponseEntity saveSetting(@RequestBody Map map){
        //2.调用service方法
        settingsService.saveSetting(map);
        //3.返回结果
        return ResponseEntity.ok(null);
    }

    //黑名单名单列表
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pagesize){
        PageResult<UserInfo> pageResult = settingsService.blacklist(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    //删除黑名单
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity removeBlacklist(@PathVariable("uid") Long id){
        settingsService.removeBlacklist(id);
        return ResponseEntity.ok(null);
    }

    //修改手机号发送短信验证码
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity updatePhone(){
        //1.获取手机号
        String mobile = UserHolder.getMobile();
        //2.发送验证
        userInfoService.sendCode(mobile);
        //3.返回结果
        return ResponseEntity.ok(null);
    }

    //校验短信验证码
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map){
        //1.获取验证码
        String verificationCode = (String) map.get("verificationCode");
        //2.调用service层方法校验验证码
        Boolean verification = userInfoService.checkVerificationCode(verificationCode);
        //3.返回结果
        Map retMap = new HashMap();
        retMap.put("verification",verification);
        return ResponseEntity.ok(retMap);
    }

    //保存手机号
    @PostMapping("/phone")
    public ResponseEntity savePhone(@RequestBody Map map){
        //1.获取手机号
        String phone = (String) map.get("phone");
        //2.调用service层修改用户手机号
        userInfoService.savePhone(phone);
        //3.返回结果
        return ResponseEntity.ok(null);
    }

    //更新头像
    @PostMapping("/header")
    public ResponseEntity header(MultipartFile headPhoto) throws IOException {
        userInfoService.updateAvatar(headPhoto);
        return ResponseEntity.ok(null);
    }

    //互相喜欢，喜欢，粉丝 - 统计
    @GetMapping("/counts")
    public ResponseEntity counts(){
        Map map = userInfoService.counts();
        return ResponseEntity.ok(map);
    }
}
